package com.itheima.reggie.web.exception;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author:wujing
 * @Date:2022/12/15 8:17
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R doSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException exception){

        /**
         * 业务异常：
         * 通知用户规范操作即可
         */
        log.info("用户操作异常：违反了数据库约束：{}" + exception.getMessage());
        String msg = exception.getMessage();
        if ("Duplicate enty".equals(exception.getMessage())){
            msg = "当前录入的数据已存在";
        }
        return R.error(msg);
    }

    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException ex){
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }


}






















